/* 
	PROJECT: scsu-csc653-chatter
	ABOUT: functions pertaining to sending and receiving messages from the chatter service
	DATE: 09-2010
	AUTHORS: skenny214@gmail.com, jinhongli08@gmail.com, hourigan.brian@gmail.com, yaqiluo2010@gmail.com
*/


//set up event listeners for message related activities
$(function() {

	//create a jquery ui button and lists for the click event
	$('#send-message-button').button()
							 .click(onSendMessageButtonClick);
							
	//attache an enter key listener for the message input button. Ease of use for the end user						
	$('#message-text-input-container').keyup(function(e){ if(e.keyCode == 13){ $('#send-message-button').click(); } });
	
});


//send message event handler
function onSendMessageButtonClick(){
	sendMessage($('#message-text-input').val());
}


//function that will be called when a messages is to be sent for the current logged in user
function sendMessage(message){
	//clear the current message value
	$('#message-text-input').val('');
	
	//send it to the applet, so it can send it to the server
	chatterClientApplet.chatterClient.chatterOutput.sendMessage(currentUsername, message);
	
}


//function that is to be called when a new message comes from the chatter server
function addMessage(username, message){
	//call the chatter applet and have it send the message
	message = filterMessage(message);
	message = emoticon(message);
	var messageListItem = $('<li class="messages-list-item">'+
							  '<span class="messages-list-item-user">'+username+'</span> - '+
						  	  '<span class="messages-list-item-message">'+message+'</span>'+
						  '</li>').hide();
						

	messageListItem.prependTo('#messages-list').slideDown(500);
	
}

//function that to filter message that has bad words.
function filterMessage(message){
	var badwords =/fuck|shit|suck/gi;
	return message.replace(badwords, function (message2) {return message2.replace(/./gi, '*');});
}


function emoticon(message){
	
	message = message.replace(/:\)/gi, "<img class='emoticon' src='images/emoticons/Smile.png'>");
	message = message.replace(/:\*/gi, "<img class='emoticon' src='images/emoticons/Adore.png'>");
	message = message.replace(/\>:\(/gi, "<img class='emoticon' src='images/emoticons/Angry.png'>");
	message = message.replace(/:\$\)/gi, "<img class='emoticon' src='images/emoticons/EasyMoney.png'>");
	message = message.replace(/xD/gi, "<img class='emoticon' src='images/emoticons/Laugh.png'>");
	message = message.replace(/:\(/gi, "<img class='emoticon' src='images/emoticons/Sad.png'>");
	message = message.replace(/:\$/gi, "<img class='emoticon' src='images/emoticons/Shok.png'>");
	message = message.replace(/:o/gi, "<img class='emoticon' src='images/emoticons/Surprise.png'>");
	message = message.replace(/;\)/gi, "<img class='emoticon' src='images/emoticons/Wink.png'>");
	message = message.replace(/:D/gi, "<img class='emoticon' src='images/emoticons/Hysterical.png'>");
	return message;
}
